package com.xgp.labblog.service.Impl;

import com.xgp.labblog.mapper.MsgMapper;
import com.xgp.labblog.mapper.MsgMapperExt;
import com.xgp.labblog.model.Msg;
import com.xgp.labblog.model.MsgExample;
import com.xgp.labblog.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class MsgServiceImpl implements MsgService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    MsgMapper msgMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    MsgMapperExt msgMapperExt;

    @Override
    public List<Msg> showMsg(int n) {

        MsgExample msgExample = new MsgExample();
        msgExample.setOrderByClause("time desc");
        List<Msg> msgs = msgMapper.selectByExample(msgExample);
        return msgs;
    }

    @Override
    public void addMsg(String msg) {
        Msg msgObj = new Msg();
        msgObj.setText(msg);
        msgObj.setTime(System.currentTimeMillis());
        msgMapper.insertSelective(msgObj);
    }

    @Override
    public List<Msg> showMsgByTitle(int i, String title) {
        MsgExample msgExample = new MsgExample();
        msgExample.setOrderByClause("time desc");
        msgExample.createCriteria().andTextLike("%" + title + "%");
        List<Msg> msgs = msgMapper.selectByExample(msgExample);
        return msgs;
    }

    @Override
    public void addSupCount(Integer id) {
        msgMapperExt.addSupCountById(id);
    }
}
